<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form label-width="220px">
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="工号">
            <el-input v-model="userInfo.workNumber" class="inputW" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              style="width: 300px"
              type="date"
              class="inputW"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 姓名 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="姓名">
            <el-input v-model="userInfo.username" class="inputW" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门">
            <el-input
              v-model="userInfo.departmentName"
              class="inputW"
              readonly
              @focus="chooseDepart = true"
            />
            <el-tree
              v-if="chooseDepart"
              :data="treeData"
              :props="defaultProps"
              @node-click="handleNodeClick"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!--手机 聘用形式  -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" style="width: 300px" readonly />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="聘用形式">
            <el-select v-model="userInfo.formOfEmployment" class="inputW">
              <el-option
                v-for="item in enumerate.hireType"
                :key="item.id"
                :label="item.value"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <up-loader ref="upLoad" :img-url="userInfo.staffPhoto" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="saveUser">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import enumerate from '@/enumerate.js'
import { getStaffDetailAPI, getDepartInfoAPI, updateStaffDetailAPI } from '@/api'
import { transTree } from '@/utils'
export default {
  name: 'UserInfo',
  data() {
    return {
      userInfo: {},
      enumerate,
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      chooseDepart: false,
      imageUrl: ''
    }
  },
  created() {
    this.getStaffDetail()
    this.getDepartInfo()
  },
  methods: {
    // 获取员工基本信息
    async getStaffDetail() {
      const res = await getStaffDetailAPI(this.$route.query.id)
      console.log(res)
      this.userInfo = res.data
      this.$set(
        this.userInfo,
        'formOfEmployment',
        Number(this.$route.query.formOfEmployment) ? Number(this.$route.query.formOfEmployment) : 2
      )
    },

    // 保存更新
    async saveUser() {
      this.userInfo.staffPhoto = this.$refs.upLoad.imageUrl
      await updateStaffDetailAPI(this.userInfo)
    },

    // 获取部门负责人列表
    async getDepartInfo() {
      const res = await getDepartInfoAPI()
      console.log(res)
      this.treeData = transTree(res.data.depts, '')
    },

    handleNodeClick(data) {
      console.log(1)
      this.userInfo.departmentName = data.name
      this.chooseDepart = false
    }
  }
}
</script>

<style lang="scss" scoped></style>
